package single_threaded;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.ExecutionException;

import GUI.GUI_MonteCarlo;
import Thread.MonteCarloThread;
public class MonteCarloSingle implements MonteCarloThread {
	@Override
	public double MonteCarloStandardOption ( String CallPutFlag, double S, double X, double T,double r, double b,double v, int nSteps, int nSimulations) throws InterruptedException, ExecutionException 
	{
		long temps=System.currentTimeMillis();
		double dt;
		double St;
		double Sum=0;
		final double Drift;
		final double vSqrdt;
		double MonteCarloStandardOption;
		final Random rand=new Random();int z=0;
		
		dt = T / nSteps;
		Drift = ((b - (v*v) )/ 2) * dt;
		vSqrdt = v * Math.sqrt(dt);
		
		if (CallPutFlag.equals("c"))
			z = 1;
		else if(CallPutFlag.equals("p"))
			z = -1;
		
		
		for(int i = 0; i<nSimulations;i++)
		{
			St=S;
			
			for(int j = 0; j<nSteps;j++)
				St=St* Math.exp(Drift + vSqrdt * rand.nextGaussian()); 
			
			Sum = Sum + Math.max(z * (St - X), 0);	
		}

		MonteCarloStandardOption = Math.exp( (-r * T) * (Sum / nSimulations));
		temps=System.currentTimeMillis()-temps;
		System.out.print("Resultat : "+MonteCarloStandardOption+" in "+temps+" millisecond");
		return MonteCarloStandardOption;
	}
	public static void main(final String[] args) {
		try {
			MonteCarloSingle m=new MonteCarloSingle();
			GUI_MonteCarlo gui=new GUI_MonteCarlo("Single-Threaded",m);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
